import * as echarts from '../../component/ec-canvas/echarts';
import Dialog from "../../../component/vant/dialog/dialog";

const app = getApp();
let chart = null;

function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);
    // var option = {
    //     title: {
    //         text: '',
    //         left: 'center'
    //     },
    //     grid: {
    //         containLabel: true
    //     },
    //     tooltip: {
    //         show: true,
    //         trigger: 'axis'
    //     },
    //     xAxis: {
    //         type: 'category',
    //         boundaryGap: false,
    //         data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    //         // show: false
    //     },
    //     yAxis: {
    //         x: 'center',
    //         type: 'value',
    //         splitLine: {
    //             lineStyle: {
    //                 type: 'dashed'
    //             }
    //         }
    //         // show: false
    //     },
    //     series: {
    //         type: 'line',
    //         smooth: true,
    //         data: [18, 36, 65, 30, 78, 40, 33]
    //     }
    // };
    //
    // chart.setOption(option);
    return chart;
}

Page({
    onShareAppMessage: function (res) {
        return {
            title: 'ECharts 可以在微信小程序中使用啦！',
            path: '/pages/index/index',
            success: function () {
            },
            fail: function () {
            }
        }
    },
    data: {
        ec: {
            onInit: initChart
        },
        cargo: {},
        cargoId: null,
        cargoNo: "",
        remark: "",
        purchasePrice: "",
        wholesalePrice: "",
        status: "",
        colors: "",
        sizes: "",
        picUrl: '',
        fileList: [],
        btnDisable: false,

        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let cargoId = options.cargoId
        // let cargoId = 279
        if (cargoId && cargoId != 'undefined' && cargoId != null) {
            this.data.cargoId = cargoId
        }
    },
    onShow() {
        let _this = this
        _this.queryData()
        setTimeout(function () {
            // 获取 chart 实例的方式
            console.log(chart)
            let options = chart.getOption();
            _this.querySalesStatData()
        }, 500);
    },
    onReady() {
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/cargo/getByCargoId?cargoId=' + this.data.cargoId, null).then(res => {
            if (res.code === 0) {
                if (res.data != null) {
                    _this.setData({
                        cargo: res.data
                    })
                }
            } else {
                app.message(res.message, 'error')
            }
        })
    },
    querySalesStatData: function () {
        let _this = this
        let data = {}
        data.cargoId = this.data.cargoId
        app.formGet('/app/cargo/sales/report', data).then(res => {
            if (res.code === 0) {
                if (res.data != null) {
                    let seriesData = res.data.seriesData
                    let xAxisData = res.data.xaxisData
                    if (seriesData.length > 0 && xAxisData.length > 0) {
                        let option = {
                            tooltip: {
                                show: true,
                                trigger: 'axis'
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                                // show: false
                            },
                            yAxis: {
                                x: 'center',
                                type: 'value',
                                splitLine: {
                                    lineStyle: {
                                        type: 'dashed'
                                    }
                                }
                                // show: false
                            },
                            series: {
                                type: 'line',
                                smooth: true,
                                data: [18, 36, 65, 30, 78, 40, 33]
                            }
                        };
                        option.xAxis = {
                            type: 'category',
                            data: xAxisData,
                            boundaryGap: false,
                            axisLabel: {
                                rotate: 45
                            }
                        }
                        option.series = [{
                            data: seriesData,
                            type: 'line',
                            smooth: true,
                        }]
                        chart.setOption(option);
                    }
                }
            } else {
                app.message(res.message, 'error')
            }
        })
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/cargo/edit/cargoEdit"
            })
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    cargoDown: function () {
        let _this = this
        let data = {}
        data.id = this.data.cargoId
        Dialog.confirm({
            title: '确认要下架?',
            message: '下架后，入库单、销售单中无法选择此商品',
        })
            .then(() => {
                app.formPost('/app/cargo/off', data).then(res => {
                    if (res.code === 0) {
                        _this.onShow()
                        app.message(res.message, 'success')
                    } else {
                        app.message(res.message, 'error')
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                // on cancel
            });
        return false;
    },
    cargoUp: function () {
        let _this = this
        let data = {}
        data.id = this.data.cargoId
        app.formPost('/app/cargo/on', data).then(res => {
            if (res.code === 0) {
                _this.onShow()
                app.message(res.message, 'success')
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
        return true;
    },
    handleCargoEdit: function () {
        wx.navigateTo({
            url: "/pages/cargo/edit/cargoEdit?cargoId=" + this.data.cargoId
        })
    }
});
